package zhihui.mobile.presenter.shop;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.order.ShopOrder;
import zhihui.mobile.bean.wx.WeixinPayInfo;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.pay.PayManager;
import zhihui.mobile.manager.shop.ShopOrderManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.order.IShopOrderManagerContrat;

/**
 * Created by 张公铭 on 2017/8/31 0031.
 */

public class ShopOrderManagerPresenter extends BaseShopPresenter implements IShopOrderManagerContrat.IShopOrderManagerPresenter
{
    private Context mContext;
    private IShopOrderManagerContrat.IShopOrderManagerView orderManagerView;
    private List<ShopOrder> mList = new ArrayList<>();

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        orderManagerView = (IShopOrderManagerContrat.IShopOrderManagerView) view;
    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void refreshList(int status)
    {
        orderManagerView.showLoading();
        String params = "0";
        switch (status)
        {
            case 0:
                params= "0";
                break;
            case 1:
                params = "1";
                break;
            case 3:
                params = "2,3,6";
                break;
            case 4:
                params = "4,7";
                break;
            case 5:
                params = "5";
        }
        ShopOrderManager.getInstance().getShopOrders(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                orderManagerView.hindLoading();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    List<ShopOrder> list = new ArrayList<>();
                    Gson gson = new Gson();
                    list = gson.fromJson(data.getJSONArray("list").toString(), new TypeToken<List<ShopOrder>>()
                    {
                    }.getType());
                    orderManagerView.refreshList(list);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    orderManagerView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                orderManagerView.hindLoading();
                orderManagerView.showMessage(message);
            }
        }, params);
    }

    @Override
    public void updateOrderStatus(final String orderId, final int status, final int viewState)
    {
        orderManagerView.showLoading();
        ShopOrderManager.getInstance().updateOrderStatus(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                orderManagerView.hindLoading();
                try
                {
                    boolean success = result.getBoolean("data");
                    if (success)
                    {
                        orderManagerView.showMessage("修改订单成功");
                        ShopOrderManager.getInstance().updateOrderListStatus(orderId, status);
                        refreshList(viewState);
                    } else
                    {
                        orderManagerView.showMessage("修改订单失败");
                    }
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(String message)
            {
                orderManagerView.hindLoading();
                orderManagerView.showMessage(message);
            }
        }, orderId, status);
    }

    @Override
    public void alipay(String orderId, int money)
    {
        if (orderId.equals("0") || money <= 0)
        {
            orderManagerView.showMessage("支付信息错误！");
            return;
        }

        orderManagerView.showLoading();
        PayManager.getInstance().ilpay(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                orderManagerView.hindLoading();
                try
                {
                    String data = result.getString("data");
                    orderManagerView.ilPay(data);

                } catch (JSONException e)
                {
                    e.printStackTrace();
                    orderManagerView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                orderManagerView.hindLoading();
                orderManagerView.showMessage(message);
            }
        }, ""+orderId, money);
    }

    @Override
    public void weixinpay(String orderId, int money)
    {
        if (orderId.equals("0") || money <= 0)
        {
            orderManagerView.showMessage("支付信息错误！");
            return;
        }

        orderManagerView.showLoading();
        PayManager.getInstance().weixin(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                orderManagerView.hindLoading();
                try
                {
                    Gson gson = new Gson();
                    WeixinPayInfo payInfo = gson.fromJson(result.getJSONObject("data").toString(), WeixinPayInfo.class);
                    orderManagerView.weixinPay(payInfo);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    orderManagerView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                orderManagerView.hindLoading();
                orderManagerView.showMessage(message);
            }
        }, ""+orderId, money);
    }
}
